﻿using SqlSugar;

namespace Sunlight.Manage.MentApi.Utility.InitDatabaseExt
{
    /// <summary>
    /// 初始化SqlSugar
    /// </summary>
    public static class InitSqlSugarExt
    {

        /// <summary>
        /// 注册SqlSugar
        /// </summary>
        /// <param name="builder"></param>
        public static void InitSqlSugar(this WebApplicationBuilder builder) {
            string? connStr = builder.Configuration.GetConnectionString("connStr");

            if (string.IsNullOrWhiteSpace(connStr))
            {
                throw new Exception("请配置数据库连接字符串");
            }

            ConnectionConfig connection = new ConnectionConfig()
            {
                ConnectionString = connStr,
                DbType = DbType.SqlServer,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute
            };

            builder.Services.AddScoped<ISqlSugarClient>(s =>
            {
                SqlSugarClient client = new SqlSugarClient(connection);

                {
                    //可以配置对于数据库操作的过滤器--SqlSugar特有的
                }

                return client;
            });
        }

    }
}
